<template>
  <div class="baseBoxInfoDiv" ref="snsBoxMain" id="smsPrintId">
    <div class="title">
      <span class="line"></span>
      <span
        ><i style="font-size: 15px" class="iconfont icon-duanxin"></i> {{$t('wechat.sms')}}
        <span style="font-size: 14px"> {{$t('wechat.smsTip')}}</span></span
      >
      <el-button
        style="float: right;margin-left: 10px;margin-right: 10px;margin-top: 6px;color: #fff;"
        type="text"
        @click="openPrintModel"
        ><i class="el-icon-printer"></i> {{$t('wechat.dayinSms')}}</el-button
      >
    </div>
    <div style="padding: 0 10px">
      <a-input-search
        :placeholder="$t('wechat.smsSearchTip')"
        v-model="searchWord"
        style="width: 300px; margin-bottom: 10px; float: right"
        @search="onSearchFirst"
      />
      <div class="contentMain">
        <div class="messageBox" ref="smsMessageRef" id="smsContentId">
          <div v-for="(item,index) in smsList" :key="index">
            <!-- right sms -->
            <div v-if="item.issend==1">
              <div class="time"><el-tag size="mini">{{formateTime(item.smstime)}}</el-tag></div>
              <div :class="item.recover==1?'leftMessageDel':'leftMessage'">
              <img class="biaozhuimgLeft" v-if="item.mark==1" width="80" height="80" src="../../../../assets/bizozhu/biaozhu.png" alt="">
              <!-- <el-button size="mini" style="float:right;" type="text">{{$t('wechat.biaoji')}}</el-button> -->
                <div class="TriangleRight"></div>
                <p style="padding-left:80px;"><i class="el-icon-user-solid"></i> {{item.name}}{{item.phone}}</p>
                <p style="padding-left:80px;">{{item.content}}</p>
                 <div>
                <div class="badgeBox" v-if="item.cntRemark>0">{{$t('wechat.remarked')}}</div>
                <el-button class="orderBtn" :title="$t('wechat.biaojitishi')" v-else @click="markOrder(item)" type="text" size="mini" style="margin-right:20px;float:right;color: #fff;" icon="el-icon-edit">{{$t('wechat.biaoji')}}</el-button>
              </div>
              </div>
            </div>
            <!-- left sms -->
            <div v-else>
              <div class="time"><el-tag size="mini">{{formateTime(item.smstime)}}</el-tag></div>
              <div :class="item.recover==1?'rightMessageDel':'rightMessage'">
              <img class="biaozhuimg" v-if="item.mark==1" width="80" height="80" src="../../../../assets/bizozhu/biaozhu.png" alt="">
                <div class="Triangle"></div>
                <p><i class="el-icon-user-solid"></i> {{item.name}}{{item.phone}}</p>
                <p>{{item.content}}</p>
                <div>
                <div class="badgeBox" v-if="item.cntRemark>0">{{$t('wechat.remarked')}}</div>
                <el-button class="orderBtn" :title="$t('wechat.biaojitishi')" v-else @click="markOrder(item)" type="text" size="mini" style="margin-right:20px;float:right;color: #000;" icon="el-icon-edit">{{$t('wechat.biaoji')}}</el-button>
              </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="offset"
          :page-sizes="[20, 30, 40, 50]"
          :page-size="limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <a-modal width="300px" v-model="showRemark" :getContainer="getRefsAff"
      :maskClosable="false" title="提示" @ok="handleOk">
      <p>{{$t('wechat.biaojibeizhu')}}</p>
      <p><el-input type="textarea" v-model="remark"></el-input></p>
    </a-modal>

    <!-- 查看短信详情 -->
    <a-modal
      v-model="modal2Visible"
      :width="500"
      :footer="null"
      title="user1(13280828937)"
      @ok="() => (modal2Visible = false)"
    >
      <el-button style="" type="text" @click="printPdfSmsContent"
        ><i class="el-icon-printer"></i> {{$t('wechat.dayinSms')}}</el-button
      >
    </a-modal>

      <!--打印窗口 -->
      <a-modal width="300px" v-model="showPrint" :getContainer="getRefsAff"
      :maskClosable="false" :title="$t('wechat.dayinyeshu')" @ok="printOk">
      <div style="display: flex;margin-bottom:20px;">
        <el-input v-model="pageIndexValue" type="number" min="1" placeholder="请输入要打印的页码"></el-input><el-button type="primary" style="margin-left:20px;" @click="addPageNumber">添加打印页码</el-button>
      </div>
     <div v-if="dynamicTags.length !==0" style="margin-bottom:20px;color:#5a5a5aa6;">已选择打印页:</div>
      <div>
      <el-tag style="margin-left:10px;" size="small" closable v-for="(item,index) in dynamicTags" :key="index" @close="closeTags">第{{ item }}页</el-tag>
      </div>
    </a-modal>
  </div>
</template>

<script>
import print from "print-js";
import html2canvas from "html2canvas";
import { getSmsApi, searchWord } from "@/api/baseInfo/base_api";
import { parseTime } from "@/utils/index";
import { sendMarkOrderApi } from '@/api/markApi/index'
export default {
  name: "sms11",
  props: ["sendData"],
  data() {
    return {
      selectType:0,
      selectTime:'',
      smsID:'',
      isExistsSelectlD:0,
      showRemark:false,
      remark:'',
      id:'',
      modal2Visible: false,
      offset: 1,
      limit: 20,
      total: 0,
      smsList: [],
      searchWord: "",
      type2:[],

      showPrint:false,
      pageIndexValue:'',
      dynamicTags:[],
    };
  },
  watch: {
    "$store.state.account.sms11": function () {
    if(this.sendData.list_offset){
      this.offset=this.sendData.list_offset;
      this.limit=this.sendData.list_limit;
    }else{
      this.offset=1
    }
      this.onSearch();
    },
  },
  created() {
    if(this.sendData.list_offset){
      this.offset=this.sendData.list_offset;
      this.limit=this.sendData.list_limit;
    }else{
      this.offset=1
    }
    this.onSearch();
  },
  methods: {
    addPageNumber(){
      if(this.pageIndexValue == '')return;
      if(this.dynamicTags.indexOf(this.pageIndexValue)==-1){
        this.dynamicTags.unshift(this.pageIndexValue);
        this.pageIndexValue = '';
        
      }else{
        this.$message({
            message: '第' + this.pageIndexValue + '页码已存在打印队列中！！',
            type: 'warning'
          });
        this.pageIndexValue = '';
      }
    },
    closeTags(tag){
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },
    openPrintModel(){
      this.dynamicTags = [];
      this.showPrint = true;
    },
    printOk(){
      this.printJson();
    },
    async printJson() {
      //通过getdata调用后台接口获取数据封装到res
      const res = await this.getPrintJsonStr();
      let that = this;
      setTimeout(()=>{
        let data = []
      for (let index = 0; index < res.length; index++) {
        const element = res[index];
              for (let i=0; i < element.list.length; i++) {
                // console.log(element);
                  const item = element.list[i];
                  // console.log(item);

                  data.push({
                    id: element.pageIndex,
                    name: item.name,
                    phone: item.phone,
                    content: item.content,
                    smstime:that.formateTime(item.smstime),
                    issend:item.issend==1?'发送':'接收',
                    recover:item.recover==1?'删除':'正常'
                  })
            }
        
      }
      printJS({
        printable: data,
        properties: [
          
          {
            field: 'name',
            displayName: '联系人姓名',
            columnSize: 1
          },
          {
            field: 'smstime',
            displayName: '时间',
            columnSize: 1
          },
          {
            field: 'phone',
            displayName: '手机号',
            columnSize: 1
          },
          {
            field: 'content',
            displayName: '短信内容',
            columnSize: 1
          },
          
          {
            field: 'recover',
            displayName: '状态',
            columnSize: 1
          },
          {
            field: 'issend',
            displayName: '接收/发送',
            columnSize: 1
          },
          {
            field: 'id',
            displayName: '页码',
            columnSize: 1
          },
        ],
        type: 'json',
        header: '通讯录',
        documentTitle:'-',
        headerStyle:'font-size:18px;',
        // 样式设置
        gridStyle: 'border: 1px solid #000;text-algin:center;',
        gridHeaderStyle: 'color:#000;  border: 1px solid #000;',
      })
      },1000)
      // console.log(res)
      // this.list = res || [];

    },    
    getPrintJsonStr(){
      // this.dynamicTags = []
      let list = []

      for (let index = 0; index < this.dynamicTags.length; index++) {
        const element = this.dynamicTags[index];

        let data={
            proof_num: this.sendData.proof_num,
            searchkey:this.searchWord,
            model:this.sendData.model,
            offset: Number(element),
            limit: this.limit,
 
   
        }
        searchWord(data,'/search/sms').then(res=>{
              let item = {
                pageIndex:element,
                list:res.data
              }
              list.push(item);
        })
        
      }
      return list;

    },






    getRefsAff(){
      return  this.$refs.snsBoxMain;
    },
    handleOk(){
        let data={
        navigation_list:sessionStorage.getItem('arr').split(','),
        model:this.sendData.model,
        isChat:0,
        app_name:'',
        account_id:'',
        proof_num:this.sendData.proof_num,
        list_offset:this.offset,
        list_limit:this.limit,
        remark:this.remark,
        class:this.sendData.class,
        label:this.sendData.label,
        id:Number(this.id)
      }
      sendMarkOrderApi(data).then(res=>{
        this.showRemark=false;
        this.$message({
          message: '标记数据成功！',
          type: 'success'
        });
        this.onSearch()
      })
    },
    // 添加标记点
    markOrder(item){
      this.id=item.id;
      this.showRemark=true;
    },
    checkboxData(){

    },
    onSearchFirst() {
      this.offset = 1;
      this.onSearch();
    },
    onSearch() {
      let data = {
        proof_num: this.sendData.proof_num,
        // model: this.sendData.model,
        searchkey: this.searchWord,
        // offset: this.offset,
        limit: this.limit,
        selectType:this.selectType,
        selectTime:this.selectTime,
        smsID:this.smsID,
        isExistsSelectlD:this.isExistsSelectlD
      };
      searchWord(data, "/search/sms_new").then((res) => {
        this.smsList = res.data;
        this.total = res.data[0].cnt;
      });
    },
    // 格式化时间
    formateTime(date) {
      return parseTime(date);
    },
    printPdfSmsContent() {
      html2canvas(document.getElementById("smsContentId")).then(function (
        canvas
      ) {
        var imgageData = canvas.toDataURL("image/png"); //把canvas转为base64图片
        // let dom = that.$refs.smsMessageRef;
        // dom.style.height = "auto";
        // dom.style['overflow-y']="visible"
        print(imgageData, "image");
      });
    },
    //打印
    printPdf() {
      html2canvas(document.getElementById("smsPrintId")).then(function (
        canvas
      ) {
        var imgageData = canvas.toDataURL("image/png"); //把canvas转为base64图片
        print(imgageData, "image");
      });
    },
    viewsSms(row) {
      this.modal2Visible = true;
    },
    handleSizeChange(val) {
      this.limit = val;
      this.onSearch();
      this.$refs.smsMessageRef.scrollTop = 0;
    },
    handleCurrentChange(val) {
      this.offset = val;
      this.onSearch();
      this.$refs.smsMessageRef.scrollTop = 0;
    },
  },
};
</script>

<style scoped>
  .orderBtn{
    position: absolute;
    top: 0px;
    right:0px;
  } 
  .orderBtn:hover{
    font-weight: bold !important;
    color: rgb(4, 92, 206) !important;;
  }
.badgeBox{
  width: 50px;
  height: 23px;
  border-radius: 10px;
  color: #fff;
  background: #f56c6c;
  text-align: center;
  line-height: 23px;
  position: absolute;
  font-size: 12px;
  top: -10px;
  right: -10px;
}
.biaozhuimg{
  position: absolute;
  right: -10px;
  transform: rotate(45deg);
  top:-10px;
}
.biaozhuimgLeft{
  position: absolute;
  left: -10px;
  transform: rotate(-45deg);
  top:-10px;
}
.smsLeftBox {
}
.contentMain {
}
.Triangle {
  position: absolute;
  width: 0;
  height: 0;
  border-bottom: 20px solid rgb(219, 219, 219);
  border-right: 20px solid transparent;
  transform: rotateZ(45deg);
  left: -10px;
  top: 10px;
}
.TriangleRight {
  position: absolute;
  width: 0;
  height: 0;
  border-bottom: 20px solid rgb(23, 192, 94);
  border-right: 20px solid transparent;
  transform: rotateZ(-135deg);
  right: -9px;
  top: 10px;
}
.closeBox {
  position: absolute;
  font-size: 0.1rem;
  right: 10px;
  top: -15px;
  cursor: pointer;
  transition: 0.1s linear;
}
.closeBox:hover {
  transition: 0.1s linear;
  transform: rotateZ(90deg);
  color: #fff;
}
.topTitle {
  display: flex;
  height: 80px;
  background: #ccc;
  line-height: 80px;
  padding: 0 0.85rem;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
}
.messageBox {
  width: 100%;
  height: 75vh;
  padding: 20px;
  overflow-y: auto;
}

.messageBox::-webkit-scrollbar {
  width: 8px;
}
/* <!--修改 滚动条的 下面 的 样式--> */
.messageBox::-webkit-scrollbar-track {
  background-color: rgb(255, 255, 255);
  -webkit-border-radius: 2em;
  -moz-border-radius: 2em;
  border-radius: 2em;
}
/* <!--修改 滑块 --> */
.messageBox::-webkit-scrollbar-thumb {
  background-color: #bbbbbb;
  -webkit-border-radius: 2em;
  -moz-border-radius: 2em;
  border-radius: 2em;
}

.smsMain {
  position: absolute;
  width: 2.7rem;
  height: 3.5rem;
  min-width: 400px;
  min-height: 500px;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
  border-radius: 5px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
}
.rightMessage {
  position: relative;
  font-size: 14px;
  float: left;
  color: rgb(51, 51, 51);
  width: 70%;
  padding: 15px;
  min-height: 50px;
  background: rgb(219, 219, 219);
  padding: 6px;
  border-radius: 4px;
  word-wrap: break-word;
  margin-bottom: 20px;
}
.rightMessageDel {
  position: relative;
  font-size: 14px;
  float: left;
  color: red;
  width: 70%;
  padding: 15px;
  min-height: 50px;
  background: rgb(219, 219, 219);
  padding: 6px;
  border-radius: 4px;
  word-wrap: break-word;
  margin-bottom: 20px;
}
.leftMessage {
  position: relative;
  font-size: 14px;
  float: right;
  color: #fff;
  width: 70%;
  padding: 15px;
  min-height: 50px;
  background: rgb(23, 192, 94);
  padding: 6px;
  border-radius: 4px;
  word-wrap: break-word;
  margin-bottom: 20px;
}
.leftMessageDel {
  position: relative;
  font-size: 14px;
  float: right;
  color: red;
  width: 70%;
  padding: 15px;
  min-height: 50px;
  background: rgb(23, 192, 94);
  padding: 6px;
  border-radius: 4px;
  word-wrap: break-word;
  margin-bottom: 20px;
}
.time {
  width: 100%;
  float: left;
  font-size: 13px;
  text-align: center;
  margin-bottom: 10px;
}
.baseBoxInfoDiv {
  width: 5rem;
}
/* .baseBoxInfoDiv{
  width: 5rem;
}
@media screen and (min-width: 2360px) {
.baseBoxInfoDiv{
  width: 3rem;
}
} */
.title {
  width: 100%;
  height: 50px;
  background: #2b2f3a;
  margin-bottom: 20px;
  font-size: 15px;
  line-height: 50px;
  padding-left: 20px;
  color: #fff;
}
.line {
  width: 8px;
  height: 100%;
}
.block {
  text-align: center;
  margin-top: 10px;
}
</style>